var panel = document.getElementById("panel"),
	ctx = panel.getContext("2d"),
	panelSize = window.innerWidth<window.innerHeight? window.innerWidth-20 : window.innerHeight-20,	
	lineCount = 15,
	gridSize = panelSize/lineCount,
	restBtn = document.getElementById("restBtn"),
	isme,
	over,
	allArray = getArray(),
	winWays = allArray.count,
	winArray = allArray.winArray,
	pieceArray = [],
	computerWinArray = [],
	myWinArray = [];
	
/*设置canvas尺寸*/	
panel.width = panelSize;	
panel.height = panelSize;	
/*初始化数据*/
init();
/*绑定事件*/
panel.onclick = play;
restBtn.onclick = init;
function init(){
	isme = true;
	over = false;
	computerWinArray.length = 0;
 	myWinArray.length = 0;
  	drawLine();
    for(var x = 0;x<15;x++){
        pieceArray[x] = [];
        for(var y = 0;y<15;y++){
            pieceArray[x][y] = 0;
        }
    }
    for(var k = 0;k<winWays;k++){
        myWinArray[k] = 0;  
        computerWinArray[k] = 0;
    } 
}
function getArray(){
    var count = 0;
    var pieceArray = [];//包含所有棋子位置
    var winArray = [];//包含所有赢法
    var myWinArray = [];//包含我方所有赢法，用来统计在该赢法上的分数
    var computerWinArray = [];//包含电脑所有赢法，用来统计在该赢法上的分数

    // 创建二维数组，用来包含所有赢法
    for(var x = 0;x<15;x++){
        winArray[x] = [];  
        for(var y = 0;y<15;y++){
            winArray[x][y] = [];
        }
    }
    // 竖线赢法
    for(var x = 0;x<15;x++){
        for(var y = 0;y<11;y++){
            for(var k = 0;k<5;k++){
                winArray[x][y+k].push(count);
            }
            count++;
        }
    } 
    // 横线赢法
    for(var x = 0;x<15;x++){
        for(var y = 0;y<11;y++){
            for(var k = 0;k<5;k++){
                winArray[y+k][x].push(count);
            }
            count++;
        }
    }   
    // 斜线赢法
    for(var x = 0;x<11;x++){
        for(var y = 0;y<11;y++){
            for(var k = 0;k<5;k++){
                winArray[x+k][y+k].push(count);
            }
            count++;
        }
    } 
    // 反斜线赢法
    for(var x = 0;x<11;x++){
        for(var y = 14;y>3;y--){
            for(var k = 0;k<5;k++){
                winArray[x+k][y-k].push(count);
            }
            count++;
        }
    } 
    for(var k = 0;k<count;k++){
        myWinArray[k] = 0;
        computerWinArray[k] = 0;
    } 
    console.log(count)
    return {
        count:count,
        winArray:winArray
    };
}
function drawLine(data){
	ctx.clearRect(0,0,panelSize,panelSize);
	ctx.strokeStyle = "#555";
    ctx.beginPath();
	for(var i=0; i<lineCount; i++){/*设置15*15的棋盘   去除上下左右的格*/
	    ctx.moveTo(gridSize/2+i*gridSize,gridSize/2);
	    ctx.lineTo(gridSize/2+i*gridSize,panelSize - gridSize/2);
	    ctx.stroke();
	    ctx.moveTo(gridSize/2,gridSize/2+i*gridSize);
	    ctx.lineTo(panelSize - gridSize/2,gridSize/2+i*gridSize);
	    ctx.stroke();
	}
}
function drawPiece(x,y){
	var posX = gridSize/2+x*gridSize;
	var posY = gridSize/2+y*gridSize;
    var rgradient = ctx.createRadialGradient(posX,posY-2,gridSize/3,posX+2,posY,0);
    if(isme){
        rgradient.addColorStop(0,"#0a0a0a");
        rgradient.addColorStop(1,"#636766");
    }else{
        rgradient.addColorStop(0,"#d1d1d1");
        rgradient.addColorStop(1,"#f9f9f9");
    }
    ctx.beginPath();
    ctx.arc(gridSize/2+x*gridSize,gridSize/2+y*gridSize,(gridSize/2-gridSize/10),0,2*Math.PI);
    ctx.fillStyle = rgradient;
    ctx.fill();
    ctx.closePath();
}
function play(e){
    if(over && !isme) return;
    var x = e.offsetX;
    var y = e.offsetY;
    x = Math.floor(x/gridSize);
    y = Math.floor(y/gridSize);
    if(pieceArray[x][y] == 0){
        drawPiece(x,y,isme);
        pieceArray[x][y] = 1;
        for(var k=0;k<winArray[x][y].length;k++){
            myWinArray[winArray[x][y][k]]++;
            computerWinArray[winArray[x][y][k]]="电脑在这个棋子上的赢法都走不通了";
            if(myWinArray[winArray[x][y][k]] == 5){
                alert("你赢了");
                 over = true;
                 break;
            }    
        }
        // 计算机出棋子
        if(!over){
            isme = !isme;
            computerPlay();
        }
    }
    
}
function computerPlay(){
    var myScore = [];
    var computerScore = [];
    var x2 = 0,y2 = 0,maxScore = 0;
    for(var x = 0;x<15;x++){
        myScore[x] = [];
        computerScore[x] = [];
        for(var y = 0;y<15;y++){
            myScore[x][y] = 0;
            computerScore[x][y] = 0;
        }
    }
    for(var x = 0;x<15;x++){
        for(var y = 0;y<15;y++){
            if(pieceArray[x][y] == 0){
                for(var k=0;k<winArray[x][y].length;k++){
                    var thisWin = winArray[x][y][k];
                    if(myWinArray[thisWin] == 1){
                        myScore[x][y] += 10;
                    }else if(myWinArray[thisWin] == 2){
                        myScore[x][y] += 100;
                    }else if(myWinArray[thisWin] == 3){
                        myScore[x][y] += 1000;
                    }else if(myWinArray[thisWin] == 4){
                        myScore[x][y] += 10000;
                    }
                    if(computerWinArray[thisWin] == 1){
                        computerScore[x][y] += 15;
                    }else if(computerWinArray[thisWin] == 2){
                        computerScore[x][y] += 150;
                    }else if(computerWinArray[thisWin] == 3){
                        computerScore[x][y] += 1500;
                    }else if(computerWinArray[thisWin] == 4){
                        computerScore[x][y] += 20000;
                    }
                    if(myScore[x][y]>maxScore){
                        maxScore = myScore[x][y];
                        x2 = x;
                        y2 = y;
                    }else if(myScore[x][y] == maxScore){
                        if(computerScore[x][y]>computerScore[x2][y2]){
                            x2 = x;
                            y2 = y;
                        }
                    }
                    if(computerScore[x][y]>maxScore){
                        maxScore = computerScore[x][y];
                        x2 = x;
                        y2 = y;
                    }else if(computerScore[x][y] == maxScore){
                        if(myScore[x][y]>myScore[x2][y2]){
                            x2 = x;
                            y2 = y;
                        }
                    }   
                }
            }
           
        }
    }

    drawPiece(x2,y2,isme);
    pieceArray[x2][y2] = 2;
    for(var k=0;k<winArray[x2][y2].length;k++){
        computerWinArray[winArray[x2][y2][k]]++;
        myWinArray[winArray[x2][y2][k]]="你在这个棋子上的赢法都走不通了";
        if(computerWinArray[winArray[x2][y2][k]] == 5){
            alert("你输了");
             over = true;
             break;
        }    
    }
     // 计算机出棋子
     if(!over){
        isme = !isme;
    }
}
